using UnityEngine;
using System.Collections;

public class FadeScale : MonoBehaviour {
	
	private Transform obj;
	private float fadeAmount;
	private FadeType fadeType;
	private float scaleI;
	private bool run = true;
	private bool turnSwitch;
	
	public void fadeInitialize(FadeType type, float amount,GameObject objectFade)
	{
		this.fadeAmount = amount;
		this.obj = objectFade.transform;
		
		switch(type)
		{
			case FadeType.FADEIN:
				obj.localScale = new Vector3(0,0,objectFade.transform.localScale.z);
				scaleI = 0;
				fadeAmount = amount;
				break;
			
			case FadeType.FADEOUT:
				scaleI = objectFade.transform.localScale.x;
				fadeAmount = -amount;
				break;
		}
		
		fadeType = type;
		StartCoroutine(runUpdate());
	}

	IEnumerator runUpdate()
	{		
		while(run)
		{
			switch(fadeType)
			{
				case FadeType.FADEIN:
					Fade();
					break;
			
				case FadeType.FADEOUT:
					Fade();
					break;
			}
			
			yield return null;
		}
	}
	
	private void Fade()
	{
		scaleI += (fadeAmount * Time.deltaTime);
		obj.localScale = new Vector3(scaleI,scaleI,transform.localScale.z);
		
		if(scaleI <= 0)
		{
			run = false;
			StopAllCoroutines();
			obj.SendMessage("actionsPostKill",SendMessageOptions.DontRequireReceiver);
			Destroy(this);
		}
	}

	public enum FadeType
	{
		FADEIN,
		FADEOUT
	}
}
